package com.tools.ad.adscene.inner;


import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.hm.adsbuisiness.bean.banner.AMNRawBannerAd;
import com.hm.adsbuisiness.bean.banner.AMRawBannerAd;
import com.hm.adsbuisiness.bean.interstitial.AMNRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.AMRawInterstitialAd;
import com.hm.adsbuisiness.bean.interstitial.FluctRawIntersitialAd;
import com.hm.adsbuisiness.bean.interstitial.MpRawInterstitialAd;
import com.hm.adsbuisiness.bean.nativead.AmUniRawNativeAd;
import com.hm.adsbuisiness.bean.nativead.RawNativeAd;
import com.hm.adsbuisiness.bean.reward.AMNRawRewardAd;
import com.hm.adsbuisiness.bean.reward.AMRawRewardAd;
import com.hm.adsbuisiness.bean.reward.FluctRawRewardAd;
import com.hm.adsbuisiness.service.AdActionException;
import com.hm.adsui.card.CommonNativeAdCardView;
import com.hm.base.android.mob.AMApplication;
import com.tools.ad.AdType;
import com.tools.ad.EntranceType;
import com.tools.ad.R;
import com.tools.ad.adscene.AdReportScene;
import com.tools.ad.adscene.InterstitialOfBannerActivity;
import com.tools.ad.adscene.InterstitialOfNativeActivity;
import com.tools.ad.strategy.IStrategyExecutor;

/**
 * desc: 普通的插屏广告场景，可以将非插屏广告类型（如：Native/Banner）以插屏广告形式展示
 * date: 2018/11/12
 * author: ancun
 */
public class CommonInterstitialActivityScene extends AdReportScene {

    protected HmAdUnionPlace mHmAdPlace;
    protected IStrategyExecutor mExecutor;

    protected int mCurrentPlaceId;

    public CommonInterstitialActivityScene(AMApplication context, EntranceType entranceType) {
        super(context, entranceType);
    }

    @Override
    protected CommonNativeAdCardView initCardView() {
        return null;
    }

    @Override
    public void onAdClose(AdType adType) {
        release();
    }

    @Override
    protected void onADLoaded(AdType adType) {
        showAd();
    }

    @Override
    public void onADShow(AdType adType) {
        super.onADShow(adType);
        if (mExecutor != null) {
            mExecutor.record();
        }
    }

    @Override
    protected void onADClick(AdType adType) {
        super.onADClick(adType);
    }

    @Override
    public void release() {
        super.release();
        mHmAdPlace = null;
    }

    /**
     * 加载广告
     *
     * @param forceLoad 是否忽略配置检查，强制加载广告
     * @return
     */
    @Override
    public boolean load(boolean forceLoad) {
        HmAdUnionPlace adUnionPlace = HmAdsSDK.getInstance().getUnionAd(mEntranceType.getPid());
        if (adUnionPlace == null) {
            return false;
        }
        mHmAdPlace = adUnionPlace;
        mCurrentPlaceId = (int) adUnionPlace.getId();
        mExecutor = getADModule().getADStrategyManager().getExecutorByEntranceType(mEntranceType);
        if (forceLoad || mExecutor.check(adUnionPlace)) {
            return loadUnionPlace(adUnionPlace);
        }

        return false;
    }

    /**
     * 显示广告
     */
    public boolean showAd() {
        boolean result = true;
        if (activeAd instanceof AMRawInterstitialAd) {
            showAmInterstitial();
        } else if (activeAd instanceof AMNRawInterstitialAd) {
            showAmNewInterstitial();
        } else if (activeAd instanceof MpRawInterstitialAd) {
            showMpInterstitial();
        } else if (activeAd instanceof AMRawBannerAd) {
            showAmBannerFullScreen();
        } else if (activeAd instanceof AMNRawBannerAd) {
            showAmNewBannerFullScreen();
        } else if (activeAd instanceof AmUniRawNativeAd) {
            showAmNewNativeFullScreen();
        } else if (activeAd instanceof RawNativeAd) {
            showNativeFullScreen();
        } else if (activeAd instanceof FluctRawIntersitialAd) {
            showFtInterstitial();
        } else if (activeAd instanceof FluctRawRewardAd) {
            showFtRewardAd();
        } else if (activeAd instanceof AMRawRewardAd) {
            showAmRewardAd();
        } else if (activeAd instanceof AMNRawRewardAd) {
            showAmNewRewardAd();
        } else {
            result = false;
        }

        return result;
    }

    /**
     * 显示 BANNER 的全屏
     */
    private void showAmBannerFullScreen() {
        InterstitialOfBannerActivity.showInterstitialAd(imContext, mEntranceType.getName(), mCurrentPlaceId);
    }

    /**
     * 显示 BANNER 的全屏
     */
    private void showAmNewBannerFullScreen() {
        InterstitialOfBannerActivity.showInterstitialAd(imContext, mEntranceType.getName(), mCurrentPlaceId);
    }

    /**
     * 显示原生的全屏
     */
    private void showNativeFullScreen() {
        InterstitialOfNativeActivity.showInterstitialAd(imContext, mEntranceType.getName(), mCurrentPlaceId);
    }

    /**
     * 显示原生的全屏
     */
    private void showAmNewNativeFullScreen() {
        InterstitialOfNativeActivity.showInterstitialAd(imContext, mEntranceType.getName(), mCurrentPlaceId);
    }

    /**
     * Admob 插屏广告
     */
    private void showAmInterstitial() {
        try {
            ((AMRawInterstitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Admob NEW 插屏广告
     */
    private void showAmNewInterstitial() {
        try {
            ((AMNRawInterstitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    /**
     * Mopub 插屏广告
     */
    private void showMpInterstitial() {
        try {
            ((MpRawInterstitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showFtInterstitial() {
        try {
            ((FluctRawIntersitialAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showFtRewardAd() {
        try {
            ((FluctRawRewardAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showAmRewardAd() {
        try {
            ((AMRawRewardAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    private void showAmNewRewardAd() {
        try {
            ((AMNRawRewardAd) activeAd).show();
        } catch (AdActionException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.full_screen_mp_card_layout;
    }

    @Override
    protected int getVideoLayoutId() {
        return R.layout.full_screen_mp_video_ad_card_layout;
    }
}
